package com.lyw.GeoLocationClient.service;

import android.content.Context;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobInstallation;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import com.lyw.GeoLocationClient.App;
import com.lyw.GeoLocationClient.bean.BmobUser;
import com.lyw.GeoLocationClient.bmob.bson.BSONObject;
import com.lyw.GeoLocationClient.bmob.restapi.BmobApi;
import timber.log.Timber;

import java.util.List;

public class BmobUserAdapter {


    private Context mContext;
    public BmobUserAdapter(Context context) {
        this.mContext = context;
    }

    public void queryBmobUser(String username, FindListener<BmobUser> listener) {
        BmobQuery<BmobUser> query = new BmobQuery<BmobUser>();
        query.addWhereEqualTo("username", username);
        query.findObjects(mContext, listener);
    }

    public void login(String username, String pwd, FindListener<BmobUser> listener) {
        BmobQuery<BmobUser> query = new BmobQuery<BmobUser>();
        query.addWhereEqualTo("username", username);
        query.addWhereEqualTo("password", pwd);
        query.findObjects(mContext, listener);
    }


    public void registerUser(String username, String password, boolean isAdmin, String pol, String name, String unit,
                             String group,String imei, SaveListener listener) {
        final BmobUser user = new BmobUser();
        user.setUsername(username);
        user.setPassword(password);
        user.setPolId(pol);
        user.setName(name);

        user.setUnit(unit);

        user.setIsAdmin(isAdmin);
        user.setImei(imei);
        if (isAdmin) {
            user.setImei(App.ADMIN_IMEI);
            user.setGroup("admin");
        } else {
            user.setGroup(group);
        }

        user.signUp(mContext, listener);
    }

    public void addGroup(String name, SaveListener listener) {
        Timber.d("add group " + name);
        final BmobUser user = new BmobUser();
        user.setUsername(name);
        user.setPassword(name);
        user.setName(name);

        user.setType(1);

        user.signUp(mContext, listener);
    }

    public void updateUser(final BmobUser user, UpdateListener listener) {
        user.update(mContext, listener);
    }

    public void queryAdmin(FindListener<BmobUser> findListener) {
        BmobQuery<BmobUser> query = new BmobQuery<BmobUser>();
        query.addWhereEqualTo("isAdmin", true);
        query.findObjects(mContext, findListener);
    }


}
